﻿using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using ST.Torch.WebApi.Core.Entities.System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;


namespace ST.Torch.WebApi.Application.System.Services
{
    public class SYSRoleService : ISYSRoleService, ITransient
	{
		private readonly IRepository<SYSRole> _SYSRoleRepository;

        public SYSRoleService(IRepository<SYSRole> SYSRoleRepository)
		{
			_SYSRoleRepository = SYSRoleRepository;
		}

		#region curd
		public  void Insert(SYSRole entity)
		{
			 _SYSRoleRepository.Insert(entity);
		}
		public async Task InsertAsync(SYSRole entity, string[] Includes=null)
		{
				await _SYSRoleRepository.InsertAsync(entity);
			
		}
		public void Update(SYSRole entity, string[] Includes = null)
		{
			if (Includes == null)
				 _SYSRoleRepository.Update(entity);
			else
				 _SYSRoleRepository.UpdateInclude(entity, Includes);
		
		}
		public async Task UpdateAsync(SYSRole entity, string[] Includes = null)
		{ 
			if (Includes == null)
				await _SYSRoleRepository.UpdateAsync(entity);
			else
				await _SYSRoleRepository.UpdateIncludeAsync(entity, Includes);
		}
		public void Delete(SYSRole entity, bool IsSoft=false)
		{
			if (IsSoft)
			{
				var fields= entity.Delete();
				Update(entity, fields);
			}
			else
			{
				_SYSRoleRepository.Delete(entity);
			}
				
		}
		public async Task DeleteAsync(SYSRole entity, bool IsSoft = false)
		{
			if (IsSoft)
			{
				var fields = entity.Delete();
				await UpdateAsync(entity, fields);
			}
			else
			{
				await _SYSRoleRepository.DeleteAsync(entity);
			}

		}
        public bool Any(Expression<Func<SYSRole, bool>> predicate)
        {
            return _SYSRoleRepository.Any(predicate);
        }
        public async Task<bool> AnyAsync(Expression<Func<SYSRole, bool>> predicate)
        {
            return await _SYSRoleRepository.AnyAsync(predicate);
        }
        public SYSRole Single(Expression<Func<SYSRole, bool>> predicate, bool tracking = false)
		{
			return _SYSRoleRepository.SingleOrDefault(predicate, tracking);

		}
		public async Task<SYSRole> SingleAsync(Expression<Func<SYSRole, bool>> predicate, bool IsIncludeMenus = false,bool IsIncludeUsers=false, bool tracking = false)
		{
			var query = _SYSRoleRepository.AsQueryable(tracking);
			if (IsIncludeMenus)
			{
				query = query.Include(u => u.Menus);
            }
			if (IsIncludeUsers)
			{
                query = query.Include(u => u.Users);
            }
            return await query.SingleOrDefaultAsync(predicate);

		}
		public List<SYSRole> Query(Expression<Func<SYSRole, bool>> predicate, bool tracking = false)
		{
            return  _SYSRoleRepository.AsQueryable(tracking).Include(u => u.Menus).Where(predicate).OrderByDescending(u => u.CreateTime).ToList();
		}
		public async Task<List<SYSRole>> QueryAsync(Expression<Func<SYSRole, bool>> predicate, bool IsIncludeMenus = false, bool tracking = false)
		{
			if (!IsIncludeMenus)
			{
				return await _SYSRoleRepository.Where(predicate, tracking).OrderByDescending(u => u.CreateTime).ToListAsync();


			}
			else
			{
                return await _SYSRoleRepository.AsQueryable(tracking).Include(u => u.Menus).Where(predicate).OrderByDescending(u => u.CreateTime).ToListAsync();
			}
		}

		public PagedList<SYSRole> QueryPage(Expression<Func<SYSRole, bool>> predicate, PageReq page, bool tracking = false)
		{
			return _SYSRoleRepository.Where(predicate, tracking).OrderByDescending(u => u.CreateTime).ToPagedList(page.page, page.limit);
		}
		public  async Task<PagedList<SYSRole>> QueryPageAsync(Expression<Func<SYSRole, bool>> predicate, PageReq page, bool tracking = false)
		{
			return await _SYSRoleRepository.Where(predicate, tracking).OrderByDescending(u => u.CreateTime).ToPagedListAsync(page.page, page.limit);
		}




        #endregion
   



    }
}
